var express = require('express');
var bodyParser = require('body-parser');
var Mock = require("mockjs");
var router = express.Router();


router.get('/', function(req, res, next) {
    var _callback = req.query.callback;
    var template = {

    }
    var data = Mock.mock({
        'list|10': [{
            'id|+1': 1,
            'color': '@color()',
            'date': '@datetime()',
            'img': '@image()',
            'url': '@url(http)',
            'email': '@email()',
            'name': "@cname(2,3)",
            'text': '@paragraph()'
        }]
    })
    if (_callback) {
        setTimeout(function() {
            res.type('text/javascript');
            res.send(_callback + '(' + JSON.stringify(data) + ')');
        }, 500);
    } else {
        res.header("Access-Control-Allow-Origin", "*");
        setTimeout(function() {
            res.send(JSON.stringify(data));
        }, 500)
    }

});

router.post('/api/detail', function(req, res, next) {
    //获取传过的参数id;
    var id = req.body.id;
    console.log(id);
    if (id) {
        var data = Mock.mock({
            'list|1': [{
                'id|+1': 1,
                'color': '@color()',
                'date': '@datetime()',
                'img': '@image()',
                'url': '@url(http)',
                'email': '@email()',
                'name': "@cname(2,3)",
                'text': '@paragraph()'
            }]
        });
        res.header("Access-Control-Allow-Origin", "*");
        setTimeout(function() {
            res.send(JSON.stringify(data));
        }, 500);
    } else {
        res.send("好像出了点问题！");
    }
})
module.exports = router;